#include<iostream>
#include<string>
using namespace std;
typedef long long LL;
string s;
int main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> s;
		bool flag = 0;
		int n = s.size();
		int cnt = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == '1') {
				flag = 1;
				cnt++;
			}
		}
		if (!flag) {
			cout << "0" << endl;
			continue;
		}
		if (cnt == s.size()) {
			cout << 1ll * cnt * cnt << endl;
			continue;
		}
		s += s;
		int ma = 0;
		int ans = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == '1') {
				ans++;
			}
			else {
				ma = max(ans, ma);
				ans = 0;
			}
		}
		//cout << "ma==" << ma << endl;
		ans = ma;
		if (ans & 1) {
			ans = (ans + 1) / 2;
			LL res = 1ll * ans * ans;
			cout << res << endl;
		}
		else {
			ans /= 2;
			LL res = 1ll * (ans + 1) * ans;
			cout << res << endl;
		}
	}
	return 0;
}